package com.kgc.easyshopping.controller;

import com.kgc.easyshopping.dto.BizDto;
import com.kgc.easyshopping.dto.SecurityUtils;
import com.kgc.easyshopping.entity.Order;
import com.kgc.easyshopping.entity.PageInfo;
import com.kgc.easyshopping.entity.Product;
import com.kgc.easyshopping.entity.User;
import com.kgc.easyshopping.service.ProductService;
import com.kgc.easyshopping.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class UserController {

    @Resource
    private RedisTemplate redisTemplate;//使用需要注入RedisTemplate
    @Autowired
    private ProductService productService;
    @Resource
    private UserService userService;


    //添加收藏商品
    @PostMapping("AddCollect")
    @ResponseBody
    public boolean AddCollect(String pid,String loginname){
        List<Product> productList = null;
        if(redisTemplate.hasKey("productList")){
            productList = (List<Product>) redisTemplate.opsForValue().get("productList");
            if(productList.size()==6){
                productList.remove(0);
            }
            Product product = productService.selectById(pid);
            productList.add(product);
        }else{
            Product product = productService.selectById(pid);
            productList = new ArrayList<>();
            productList.add(product);
        }
        redisTemplate.opsForValue().set(loginname,productList);//存储
        return true;
    }

    //移除收藏商品
    @PostMapping("DelCollect")
    public boolean delCollect(String pid,String loginname){
        List<Product> productList = (List<Product>) redisTemplate.opsForValue().get(loginname);
        for(int i=0;i<productList.size();i++){
            if(pid.equals(productList.get(i).getPid())){
                productList.remove(i);
            }
        }
        redisTemplate.opsForValue().set(loginname,productList);//存储
        return true;
    }

    //分页展示收藏商品
    @PostMapping("showCollect")
    public PageInfo<Order> selectPageInfo(@RequestParam Map<String, Object> params){
        Map<String, Object> map = new HashMap<>();

        int pageNow =  Integer.parseInt((String) params.get("pageNow"));
        int pageSize = Integer.parseInt((String) params.get("pageSize"));
        map.put("pageNow", pageNow);
        map.put("pageSize", pageSize);

        String name = (String) params.get("loginname");
        if(name!=null && name!=""){
            map.put("loginname",name);
        }
        BizDto<PageInfo> bizDto = new BizDto<PageInfo>();
        bizDto.setData(userService.selectPageInfo(map));
        return bizDto.getData();
    }

    //个人信息修改和密码修改

    @GetMapping("UserShow")
    @ResponseBody
    public User UserShow(String uid){
        return userService.selectByPrimaryKey(uid);
    }

    @GetMapping("isExistName")
    @ResponseBody
    public boolean isExistName(String uid,String loginname){
        return userService.selectUser(loginname,uid)==1?false:true;
    }

    //校验原密码
    @GetMapping("isExistPassword")
    public boolean isExistPassword(String uid,String password){
        User user = userService.selectByPrimaryKey(uid);
        boolean flag = false;
        if(user.getPassword().equals(SecurityUtils.md5Hex(password))){
            flag = true;
        }
        return flag;
    }

    @GetMapping("isExistEmail")
    public boolean isExistEmail(String uid,String email){
        User user = userService.selectByPrimaryKey(uid);
        boolean flag = false;
        if(user.getEmail().equals(email)){
            flag = true;
        }
        return flag;
    }

    @GetMapping("isExistPhone")
    public boolean isExistPhone(String uid,String mobile){
        User user = userService.selectByPrimaryKey(uid);
        boolean flag = false;
        if(user.getMobile().equals(mobile)){
            flag = true;
        }
        return flag;
    }

    @PostMapping("UserUpdate")
    @ResponseBody
    public String UserUpdate(String uid,String loginname,String sex){
        String flag = null;
        if(userService.selectUser(loginname,uid)==1){
            flag = "登陆用户名重复，请重新输入";
        }else{
            User user = new User();
            user.setUid(uid);
            user.setSex(Integer.parseInt(sex));
            flag = userService.updateByPrimaryKeySelective(user)==1?"修改成功":"修改失败";
        }
        return  flag;
    }

    @PostMapping("PhoneUpdate")
    public int PhoneUpdate(@RequestBody User user){
        return userService.updateByPrimaryKeySelective(user);
    }

    @PostMapping("EmailUpdate")
    public int reviseEmail(@RequestBody User user){
        return userService.updateByPrimaryKeySelective(user);
    }

    //修改密码
    @PostMapping("PasswordUpdate")
    public int PasswordUpdate(@RequestBody User user){
        user.setPassword(SecurityUtils.md5Hex(user.getPassword()));
        return userService.updateByPrimaryKeySelective(user);
    }

}
